package cn.nb12.okhttp3.service.impl;

import org.springframework.stereotype.Service;

import com.google.gson.Gson;

import cn.nb12.okhttp3.po.Root;
import cn.nb12.okhttp3.service.WeatherService;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

@Service
public class WeatherServiceImpl implements WeatherService {

	@Override
	public Root getFindCodeOkHttp3(String code) {

		String url = "http://www.weather.com.cn/data/cityinfo/" + code + ".html";

		OkHttpClient client = new OkHttpClient();

		Request request = new Request.Builder().url(url).get().build();

		Root root = null;
		try {

			Response response = client.newCall(request).execute();

			Gson gson = new Gson();
			root = gson.fromJson(response.body().string(), Root.class);

		} catch (Exception e) {
			e.printStackTrace();

		}
		return root;
	}

}
